package test;

import java.util.Arrays;


public class Solution {
    public static void main(String[] args) {
        /*int[][] ints1 = new int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12}};
        int[] ints = SpiralMatrix(ints1);
        String s = Arrays.toString(ints);
        System.out.println(s);*/

        int[] ints = {1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0};
        int i = GetMaxConsecutiveOnes(ints, 2);
        System.out.println(i);
    }

    //循环打印二维矩阵
    public static int[] SpiralMatrix (int[][] matrix) {
        // write code here
        int line = matrix.length; //行
        int column = matrix[0].length; //列
        int l=0,r=column-1,t=0,b=line-1;//左右上下边界
        int[] ints = new int[line * column];
        int index = 0;
        while (index < ints.length) {
            //右下左上 遍历
            for (int i = l; i <= r; i++) {
                ints[index++] = matrix[l][i];
            }
            t++;
            if (t>b) break;

            for (int i = t; i <= b; i++) {
                ints[index++] = matrix[i][r];
            }
            r--;
            if (r<l) break;


            for (int i = r; i >= l; i--) {
                ints[index++] = matrix[b][i];
            }
            b--;
            if (b<t) break;

            for (int i = b; i >= t; i--) {
                ints[index++] = matrix[i][l];
            }
            l++;
            if (l>r) break;
        }
        return ints;
    }

    //返回仅包含 1 的最长（连续）子数组的长度
    public static int GetMaxConsecutiveOnes(int[] arr, int k) {
        // write code here

        int left =-1;//左指针
        int right = 0;//右指针
        int maxLength = 0;//最大长度

        //将k个0变为1
        while (right<arr.length) {
            if (arr[right] == 0) {
                while (k > 0) {
                    arr[right] = 1;
                    right++;
                    k--;
                }
            }
            right++;
        }
        right = 0;//重置right
        while (right<arr.length){
            if(arr[right] == 0){
                left = right;
            }
            if (arr[right] == 1){
                maxLength = Math.max(maxLength,right -left);
            }
            right++;
        }
        return maxLength;

        /*k++;
        int[][] dp = new int[arr.length][k];
        for (int i = 0; i < dp.length; i++) {
            for (int j = 0; j < k; j++) {
                if (arr[i] == 1) {
                    dp[i][j] = i > 0 ? dp[i - 1][j] + 1 : 1;
                } else {
                    dp[i][j] = j > 0 && i > 0 ? dp[i - 1][j - 1] + 1 : 0;
                }
            }
        }
        int max = 0;
        for (int i = 0; i < dp.length; i++) {
            max = Math.max(dp[i][k - 1], max);
        }
        return max;*/
    }
}
